package com.easyfun.healthmagicbox.auth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.base.BaseActivity;
import com.easyfun.healthmagicbox.comm.ForgetPasswordTask;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.comm.UserLoginTask;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.sync.CommCallback;

public class ForgetPasswordActivity extends BaseActivity{
	
	TextView usernameview;
	TextView emailview;
	protected CommCallback mcallback = new BasicCommCallback();
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pageforgetpassword);
		
		//addbackButton should after setting content view
		addbackButton(this);
		
		usernameview = (TextView) findViewById(R.id.pagefp_login_user_edit);
		emailview = (TextView) findViewById(R.id.pagefp_login_passwd_edit);
		
		String m_username = getIntent().getStringExtra(ConstantData.HMBUSERNAME);
		usernameview.setText(m_username);
		
		Button loginbut = (Button) findViewById(R.id.pagefp_login_but);
		loginbut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final Intent intent = new Intent(ForgetPasswordActivity.this, LoginActivity.class);
				intent.putExtra(ConstantData.HMBUSERNAME,usernameview.getText());
				ForgetPasswordActivity.this.startActivity(intent);
			}
		});
		
		Button fpbut = (Button) findViewById(R.id.pagefp_regist_but);
		fpbut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ForgetPasswordTask task = new ForgetPasswordTask(
						ForgetPasswordActivity.this, 
						usernameview.getText().toString(), 
						emailview.getText().toString(),
						mcallback );
				HealthThreadPool.getInstance().execute(task);
				String responseValue = "";
				int i = 0;
				ServerResponseCode src = mcallback.getCommResult(ForgetPasswordTask.class, responseValue);
				while ((src==ServerResponseCode.WAITING) && (i<20))
				{
					synchronized (mcallback) {
						try {
							mcallback.wait(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					src = mcallback.getCommResult(UserLoginTask.class, responseValue);
					i++;
				}
				switch (src)
				{
				case SUCCESS:
					Toast.makeText(getApplicationContext(),
							"请接受密码重置邮件",
							Toast.LENGTH_SHORT).show();;
				case NOMORE:
					Toast.makeText(getApplicationContext(),
							"用户名，email 不匹配",
							Toast.LENGTH_SHORT).show();;
				default:
					Toast.makeText(getApplicationContext(),
							"网络故障",
							Toast.LENGTH_SHORT).show();;
				}
			}
		});
	}
}
